<?php

namespace DebugComponent\Plugin;

class Log implements \DebugComponent\IMessagePlugin {

    public function  __construct(\DebugComponent\IStorage $storage, \DebugComponent\IFilter $filter=null, \DebugComponent\IFormatter $formatter=null) {
        $this->storage = $storage;
        $this->formatter = $formatter;
        $this->filter = $filter;        
    }

    public function getListenMessages() {
        return array('\DebugComponent\IMessage');
    }

    public function write(\DebugComponent\IMessage $message) {
        if ($this->filter instanceof \DebugComponent\IFilter) {
            if ($this->filter->filter($message) !== true) {
                return;
            }
        }        
        if ($this->formatter instanceof \DebugComponent\IFormatter) {
            $this->storage->write($this->formatter->format($message));
        } else {
            $this->storage->write($message);
        }
    }

    private $formatter;
    private $storage;
    private $filter;
}